<template>
  <!--1.首先，弹窗页面中要有el-dialog组件即弹窗组件，我们把弹窗中的内容放在el-dialog组件中-->
  <!--2.设置:visible.sync属性，动态绑定一个布尔值，通过这个属性来控制弹窗是否弹出-->
  <el-dialog title="编辑书本" :visible.sync="detailVisible" class="dialog">
    <div >
      <form>
        <div class="outline">
          <el-col class="inputImg-col"><!--上传书籍封面-->
            <el-row class="inputImg-gray"  ref="inputImg"  :style="{ 'background-image': `url(${filePath})`,'background-size': 'cover' }">
              <label for="upload" class="bookImg-font">上传书籍封面</label>
              <input id="upload" type="file" style="display: none;" @change="handleFileUpload">
            </el-row>
          </el-col>
          <div class="center">
            <div class="line">
              <label for="name" >书籍名称:</label>
              <input id="name" v-model="formMsg.name">
            </div>
            <div class="line">
              <label for="writer" >作者:</label>
              <input id="writer" v-model="formMsg.writer">
            </div>
            <div class="line">
              <label for="print" >出版社:</label>
              <input id="print" v-model="formMsg.publisher">
            </div>
            <div class="line">
              <label for="date" >出版日期:</label>
              <input id="date" v-model="formMsg.date">
            </div>
            <div class="line">
              <label for="code" >书记条形码:</label>
              <input id="code" v-model="formMsg.code">
            </div>
            <div class="line">
              <label for="type" >书籍类型:</label>
              <input id="type" v-model="formMsg.type">
            </div>
            <div class="line">
              <label for="type" class="label-left" >来源:</label>
              <input id="type" v-model="formMsg.source">
            </div>
            <div class="line">
              <label for="type" class="label-left" >库存:</label>
              <input id="type" v-model="formMsg.stock">
            </div>
            <div class="line">
              <label for="type" class="label-left">价格:</label>
              <input id="type" v-model="formMsg.price">
            </div>
          </div>
          <div class="right">
            <input type="button" value="保存" class="store" @click="onSubmit">
            <br>
            <input type="button" value="取消" class="cancel" @click="detailVisible=false">
          </div>
        </div>
      </form>
    </div>
  </el-dialog>
</template>

<script>
import axios from "axios";

export default {
  name: "dialogComponent",
  data() {
    return {
      detailVisible: false,
      filePath:"",
      fileName:"",
      formMsg:{
        name:"",
        code:"",
        type:"",
        writer:"",
        date:"",
        publisher:"",
        stock:"",
        price:"",
        source:"",
      },
      admin_id:"27",
    }
  },
  methods: {
    //3.定义一个init函数，通过设置detailVisible值为true来让弹窗弹出，这个函数会在父组件的方法中被调用
    init(row) {
      this.detailVisible = true;
      this.formMsg.name=row.name
      this.formMsg.code=row.book_code
      this.formMsg.type=row.type
      this.formMsg.writer=row.author
      this.formMsg.date=row.publish_time
      this.formMsg.publisher=row.publisher
      this.formMsg.stock=row.inventory
      this.formMsg.price=row.price
      this.formMsg.source=row.source
    },
    handleFileUpload(event){
      const file = event.target.files[0];
      this.fileName = file.name.replace(/\.[^/.]+$/, ''); // 移除文件扩展名
      console.log('文件名',this.fileName)//数据库存的
      this.filePath=require("D:/librarycover/" + this.fileName + ".jpg")
    },
    onSubmit(){
      axios({
        method:"get",
        url:"http://localhost:5000//admin/book/edit",
        params:{
          name:this.formMsg.name,
          code:this.formMsg.code,
          type:this.formMsg.type,
          writer:this.formMsg.writer,
          date:"2022-2-2",
          press:this.formMsg.publisher,
          stock:this.formMsg.stock,
          price:this.formMsg.price,
          source:this.formMsg.source,
          picture:this.fileName,
          admin_id:"27",

        }
      }).then((res)=> {
        if(res.data.data ==='书籍信息更新成功'){
          this.$router.go(0)
          this.$message({
            message: res.data.data,
            type:"success"
          })
          this.detailVisible = false
          //这里需要手动刷新
        }else {
          this.$message({
            message: res.data.data,
            type: "info"
          })

        }
      })
    }
  }
}
</script>
<style scoped>
.dialog{
  width:100%;
  border-radius: 25px;
  padding: 10px;
  margin:auto;
}
.outline{
  padding-left: 5%;
  //display: flex;
  //flex-direction: column;
  width: 450px;
  height: 320px;
}
.outline>div{
  margin: 3%;
}
label{
  vertical-align: top;
  margin-right: 2%;
}
.center{
  position: relative;
  width: 200px;
  height: 150px;
  padding:10px;
  left: 50px;
  top:-50px
}
#title{
  border-radius: 15px;
  background-color: rgb(237,237,237);
  margin-left: 5px;
  border: none;
  padding: 5px;
}
#content{
  width: 260px;
  height: 100px;
  resize: none ;
  border-radius: 15px;
  background-color: rgb(237,237,237);
  margin-left: 5px;
  border: none;
  padding-left: 5px;
  padding-right: 5px;
  font-family: "Helvetica Neue",Helvetica,"PingFang SC","Hiragino Sans GB","Microsoft YaHei","微软雅黑",Arial,sans-serif;

}
.cancel{
  display: inline-block;
  width: 100px;
  height: 40px;
  margin: 10px;
  position: relative;
  left: 40%;
  background-color: rgb(166,166,166);
  border-radius: 15px;
  border: none;
  color: white;
  cursor: pointer;
}
.cancel:hover{
  background-color: red;
}
.store{
  display: inline-block;
  width: 100px;
  height: 40px;
  margin: 10px;
  position: relative;
  left: 40%;
  background-color: rgb(125,153,191);
  border-radius: 15px;
  border: none;
  color: white;
  cursor: pointer;
}
.store:hover{
  background-color: rgb(85,153,191);
}
.bookImg-font{
  font-size:15px;
  color:rgba(128, 128, 128, 1);
  cursor: pointer;
}
.inputImg-col{
  height:70%;
  width:30%;
  display: flex;
  justify-content: center;
  align-items: center;

}
.inputImg-gray{
  width:85%;
  height:78%;
  background-color:rgba(237, 237, 237, 1);
  border-radius: 15px;
  margin-left:20px;
  display: flex;
  justify-content: center;
  align-items: center;
}

.right{
  display: inline-block;
  width: 400px;
  height: 100px;
  position: relative;
  top: -230px;
  right: -320px;
}
div.line{
  width: 340px;
  padding: 10px;
}
div.line>input{
  background-color:rgb(237,237,237) ;
  border-radius: 15px;
  border: none;
  width: 120px;
  float: right;
  height: 25px;
  padding-left: 5px;
}
div.line>label{
  width: 50px;

}
.label-left{
  position: relative;
  left: 130px;
}
</style>